home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / post1.0 / ui / example-swing / LabelDemo.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  2.2 KB  |  69 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.GridLayout;
  18. import java.awt.event.WindowListener;
  19. import java.awt.event.WindowAdapter;
  20. import java.awt.event.WindowEvent;
  21. import com.sun.java.swing.JLabel;
  22. import com.sun.java.swing.JPanel;
  23. import com.sun.java.swing.JFrame;
  24. import com.sun.java.swing.ImageGlyph;
  25. import com.sun.java.swing.GraphicsUtilsConstants;
  26.  
  27. public class LabelDemo extends JPanel {
  28.  
  29. JLabel label1, label2, label3;
  30.  
  31.     public LabelDemo() {
  32.     super(true);                 // true = please double buffer
  33.     ImageGlyph glyph = new ImageGlyph("middle.gif");
  34.     setLayout(new GridLayout(3,1));     //3 rows, 1 column
  35.  
  36.         label1 = new JLabel("Image and Text",
  37.                 glyph,
  38.                 GraphicsUtilsConstants.CENTER);
  39.     label1.setVerticalTextPosition(GraphicsUtilsConstants.BOTTOM);
  40.     label1.setHorizontalTextPosition(GraphicsUtilsConstants.CENTER);
  41.  
  42.         label2 = new JLabel("Text-Only Label");
  43.  
  44.         label3 = new JLabel(glyph);
  45.  
  46.         //Add labels to the JPanel. 
  47.         add(label1);
  48.         add(label2);
  49.         add(label3);
  50.     }
  51.  
  52.     public static void main(String[] args) {
  53.     /*
  54.      * Create a window.  Use JFrame since this window will include 
  55.      * lightweight components.
  56.      */
  57.     JFrame frame = new JFrame("LabelDemo");
  58.  
  59.     WindowListener l = new WindowAdapter() {
  60.         public void windowClosing(WindowEvent e) {System.exit(0);}
  61.     };
  62.     frame.addWindowListener(l);
  63.  
  64.     frame.add("Center", new LabelDemo());
  65.     frame.pack();
  66.     frame.show();
  67.     }
  68. }
  69.